//
//  AddCemeralViewController.m
//  wAudioShare
//
//  Created by Ning on 14-7-30.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "AddCemeralViewController.h"
#import "Utitlity.h"
#import "MMProgressHUD.h"

@interface AddCemeralViewController ()<UITextFieldDelegate>
{
    NSString *ipUrl;
    NSString *cameraPort;
    NSMutableDictionary *data;
    NSArray *manuArray;
    NSArray *iProductArray;
    NSArray *aProductArray;
    NSInteger currentCom;
    NSInteger currentRow;
    UIPickerView *cameraPkView;
    NSString *manuStr;
    NSString *productStr;
}

@end

@implementation AddCemeralViewController
@synthesize portText;
@synthesize aliasText;
@synthesize ipText;
@synthesize subPortText;
@synthesize chooseDeviceBtn;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"caremaList" ofType:@"plist"];
    data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
    if (data)
    {
        NSLog(@"data:%@",data);
    }
    manuArray = [[NSArray alloc] initWithObjects:@"unknow",@"iView", nil];
    iProductArray = [[NSArray alloc] initWithObjects:@"T6836WITP",nil];
//    aProductArray = [[NSArray alloc] initWithObjects:@"All-Type", nil];
    [chooseDeviceBtn addTarget:self action:@selector(chooseDeviceInfo) forControlEvents:UIControlEventTouchUpInside];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
}

- (void)chooseDeviceInfo
{
    UIAlertView *errorAlertV = [[UIAlertView alloc] initWithTitle:@"error！" message:@"请添加全部信息" delegate:self cancelButtonTitle:@"确认" otherButtonTitles: nil];
    
    if ([ipText.text isEqualToString:@""]||[portText.text isEqualToString:@""]||[aliasText.text isEqualToString:@""]||[subPortText.text isEqual:@""])
    {
        [errorAlertV show];
        return;
    }
    NSString *addCameraStr = [NSString stringWithFormat:@"http://%@/cgi-bin/IpCamera.cgi?OP=1&Alias=%@&Manufacturer=2&Product=201&IP=%@&Port=%@&PubPort=%@&Username=admin&Password=admin",ipUrl,aliasText.text,ipText.text,portText.text,subPortText.text];
    
    ipUrl = [Utitlity judgeipUrl];
    if (ipUrl)
    {
        NSString *addCameraStr = [NSString stringWithFormat:@"http://%@/cgi-bin/IpCamera.cgi?OP=1&Alias=%@&Manufacturer=2&Product=201&IP=%@&Port=%@&PubPort=%@&Username=admin&Password=admin",ipUrl,aliasText.text,ipText.text,portText.text,subPortText.text];
         NSString * encodingString = [addCameraStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        
        NSLog(@"addIpcamera:%@",encodingString);
        
        [Utitlity commonHTTPMethod:encodingString responseType:jsonType success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSLog(@"responseObjectForAdd:%@",responseObject);
            
            if (self.delegate)
            {
                [self.delegate loadCameraList];
            }
            
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"error:%@",error);
        }];
    }
    [self.navigationController popViewControllerAnimated:YES];

}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)dismissKeyboard:(id)sender
{
    [self resignResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self resignResponder];
    return YES;
}

- (void)resignResponder
{
    [aliasText resignFirstResponder];
    [ipText resignFirstResponder];
    [portText resignFirstResponder];
    [subPortText resignFirstResponder];
}

@end
